package com.zhanlv.composer

import android.util.Log
import java.lang.Exception
import java.net.Socket


object SocketClient {
    private var socket: Socket? = null

    const val TAG = "SocketClient"

    fun connect(ip: String, port: Int){
        Thread(Runnable {
            try {
                socket = Socket(ip, port)
                ClientWorkThread(socket!!).start()
            }catch (e: Exception){
                Log.e(TAG, "connect error: " + e.message)
            }

        }).start()
    }

    fun request(paramsJsonStr: String){
        Thread(Runnable {
            socket?.let {
                if (it.isClosed) return@Runnable

                val outputStream = it.getOutputStream()
                outputStream.write(paramsJsonStr.toByteArray())
                outputStream.flush()
            }
        }).start()
    }
}

